package com.xiaohudie.aop;

import cn.hutool.core.bean.BeanUtil;
import com.xiaohudie.annotation.LJL;
import com.xiaohudie.pojo.AjaxResult;
import com.xiaohudie.pojo.MyThreadLocal;
import com.xiaohudie.pojo.User;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @Author xiao hu die
 * @Date 2022/3/2 17:06
 * @Version 1.0
 */
/**局部拦截器*/
public class LoginBlocker implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    /**只需要判断是否有这个用户*/
        User user = MyThreadLocal.getUser();
        /**获得具体的拦截方法
         * 用于将某些带有特定注解的方法放开拦截*/
      /*  HandlerMethod handlerMethod = (HandlerMethod) handler;
        Method method = handlerMethod.getMethod();
        String name = method.getName();
        LJL annotation = method.getAnnotation(LJL.class);
        if (null != annotation){
            return  true;
        }*/
        if (user==null){
            /**response中返回值*/
            response.setContentType("text/html;charset=UTF-8");
            AjaxResult ajaxResult = new AjaxResult();
            ajaxResult.setCode(201);
            ajaxResult.setMsg("请先登录");
            ajaxResult.setData(" ");
            String s = ajaxResult.toString();
            response.getWriter().write(s);
            response.setStatus(401);
            return false;
        }
        return true;
    }


}
